home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6190 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: nntp.teleport.com!usenet
  2. From: GHouck <hksys@teleport.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [Help] I can't find my error.
  5. Date: 23 Feb 1996 01:34:25 GMT
  6. Organization: systems hk
  7. Message-ID: <4gj5j2$eqm@maureen.teleport.com>
  8. References: <4ggvgr$1b2@aurora.engr.LaTech.edu>
  9. NNTP-Posting-Host: ip-pdx02-33.teleport.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. pluu@engr.LaTech.edu (PL) wrote:
  16. >Could anyone please tell me why the following program won't work?
  17. >*********************************************************
  18. >#include <stdio.h>
  19. >main()
  20. >{
  21. >    char name;    
  22. >    int  age, next_age; 
  23. >
  24. >    printf("%s\n","Please enter your name and age: ");
  25. >    scanf("%s%d\n", &name, &age);
  26. >    next_age= age + 1;
  27. >    printf("Hi, %s ,next year, you will be %d\n", name, next_age);
  28. >}
  29. >*********************************************************
  30. >
  31. Hi back!
  32. You need to dimension your 'name' string (array of characters) to allow
  33. for input of the name string.  Also, change the scanf to use the address
  34. of the array (via its name):
  35.  
  36.   #define  MAXNAMELEN  128     /* for example...  */
  37.   ...
  38.   char  name[MAXNAMELEN];      /* alloc an array of chars   */
  39.   ...
  40.   scanf( "%s %d",name,&age );  /* remove '\n' and '&' operator */
  41.  
  42. Hope I didn't make too many mistakes; there are sharks in the waters.
  43. Yours, Geoff Houck
  44.  
  45.